package com.glodon.jcloud.increment.shorturl.exception;

import com.glodon.jcloud.common.api.APIResponse;
import com.glodon.jcloud.common.exception.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Create Date: 2022/7/8
 *
 * @description 广联达 - 增值业务
 * @version: V8.3.4-SNAPSHOT
 * @author: Terry.Li
 * @mail: lib-o@glodon.com
 */
@Slf4j
@RestControllerAdvice
public class ApiExceptionHandler {

    @ExceptionHandler(value = {ApiException.class})
    public APIResponse<String> resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        if (ex instanceof ApiException) {
            return APIResponse.error(((ApiException) ex).getStatus());
        }
        return APIResponse.error();
    }
}
